<template>
  <div>
    <nav-bar title="添加报备"></nav-bar>
    <div class="maxinBox">
      <van-cell required is-link title="参建单位" :value="unitName" @click="showQuistionType = true" />
      <van-action-sheet v-model="showQuistionType" :actions="unitList" @select="onSelect" />
      <van-field
        required
        v-model="form.name"
        name="姓名"
        label="姓名"
        placeholder="请输入姓名"
      />
      <van-field
        required
        v-model="form.idCardNo"
        name="身份证号"
        label="身份证号"
        placeholder="请输入身份证号"
      />
      <div class="backgroundfff" style="position: fixed;bottom: 0;width: 100%;">
        <div style="padding: 0.3125rem 10%;">
          <van-button round block type="info" @click="addReported">
            确认添加
          </van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import personalManageAPI from '../../../../api/workAPI/personalManageAPI.js'
  import dashbordAPI from '../../../../api/dashbordAPI.js'
  export default {
    data() {
      return {
        unitList: [],
        showQuistionType: false,
        unitName: '',
        form: {
          name: '',
          idCardNo: '',
          unitId: ''
        }
      }
    },
    methods: {
      // 获取参建单位列表
      getUnitList() {
		let projectId = localStorage.getItem('projectId')
        dashbordAPI.getUnit(projectId).then(res => {
          this.unitList = res.data.map(item => {
            return {
              name: item.dictLabel,
              value: item.dictCode
            }
          })
        })
      },
      // 选择问题类别
      onSelect(item) {
        this.showQuistionType = false;
        this.form.unitId = item.value
        this.unitName = item.name
      },
      // 新增报备
      addReported() {
        if (this.form.name === '') {
          this.$toast.fail('人员姓名不能为空')
          return
        }
        if (!(/^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/.test(this.form.name))) {
          this.$toast.fail('请输入正确的姓名')
          return
        }
        if (this.form.idCardNo === '') {
          this.$toast.fail('身份证号码不能为空')
          return
        }
        if (!(/^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/.test(this.form.idCardNo))) {
          this.$toast.fail('请输入正常的身份证号码')
          return
        }
        if (this.form.unitId === '') {
          this.$toast.fail('请选择参见单位')
          return
        }
        let projectId = localStorage.getItem('projectId')
        personalManageAPI.addReported(projectId, this.form).then(res => {
          this.$toast('添加成功')
          this.$router.push({
            name: 'PersonalManage'
          })
        })
      }
    },
    mounted() {
      this.getUnitList()
    }
  }
</script>

<style scoped lang="scss">

</style>
